import axios from 'axios'
import {Message} from 'element-ui'

// 创建axios实例
const service = axios.create({
  timeout: 50000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  // Indicator.open({
  //   spinnerType: 'fading-circle'
  // })
  config.headers = {'Content-Type': 'application/x-www-form-urlencoded'}

  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    console.log(response.data)
    if (response.data.errno != 0) {
      return Message({
        message: response.data.errmsg,
        type: 'error',
        duration: 5 * 1000
      })
    } else {
      return response
    }
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
